package com.nantong.admin.service.impl;

import java.util.List;
import java.util.Map;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;

import com.nantong.admin.domain.Customer;
import com.nantong.admin.domain.model.request.customer.CustomerSearch;
import com.nantong.admin.domain.model.response.customer.CustomerVo;
import com.nantong.admin.mapper.CustomerMapper;
import com.nantong.admin.service.ICustomerService;
import com.nantong.common.core.utils.StringUtils;
import com.nantong.common.mybatis.core.page.PageQuery;
import com.nantong.common.mybatis.core.page.TableDataInfo;

import org.springframework.stereotype.Service;

/**
 * 客户Service实现类
 */
@Service
@RequiredArgsConstructor
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {

    private final CustomerMapper customerMapper;

    @Override
    public TableDataInfo<CustomerVo> selectPageList(CustomerSearch customerSearch, PageQuery pageQuery) {
        Page<CustomerVo> page = customerMapper.selectPageList(pageQuery.build(), buildQueryWrapper(customerSearch));
        return TableDataInfo.build(page);
    }

    @Override
    public List<CustomerVo> searchList(CustomerSearch customerSearch) {
        Wrapper<Customer> customerWrapper = this.buildQueryWrapper(customerSearch);
        return customerMapper.selectVoList(customerWrapper);
    }

    @Override
    public Customer selectCustomerById(Long customerId) {
        return this.getById(customerId);
    }

    @Override
    public int insertCustomer(Customer customer) {
        return this.save(customer) ? 1 : 0;
    }

    @Override
    public int updateCustomer(Customer customer) {
        return this.updateById(customer) ? 1 : 0;
    }

    @Override
    public int deleteCustomerById(Long customerId) {
        return this.removeById(customerId) ? 1 : 0;
    }

    private Wrapper<Customer> buildQueryWrapper(CustomerSearch customerSearch) {
        Map<String, Object> params = customerSearch.getParams();
        LambdaQueryWrapper<Customer> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotBlank(customerSearch.getCompanyName()), Customer::getCompanyName,
                        customerSearch.getCompanyName())
                .like(StringUtils.isNotBlank(customerSearch.getCustomerName()), Customer::getCustomerName,
                        customerSearch.getCustomerName())
                .like(StringUtils.isNotBlank(customerSearch.getPhone()), Customer::getPhone, customerSearch.getPhone())
                .eq(ObjectUtil.isNotNull(customerSearch.getSource()), Customer::getSource, customerSearch.getSource())
                .eq(ObjectUtil.isNotNull(customerSearch.getLevel()), Customer::getLevel, customerSearch.getLevel())
                .eq(ObjectUtil.isNotNull(customerSearch.getProgressStage()), Customer::getProgressStage,
                        customerSearch.getProgressStage())
                .between(params.get("beginTime") != null && params.get("endTime") != null,
                        Customer::getCreateTime, params.get("beginTime"), params.get("endTime"));
        return wrapper;
    }
}
